<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>农汇购-新增类型</title>
<link href="${pageContext.request.contextPath }/css/bootstrap.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath }/layui/css/layui.css" rel="stylesheet">
<script src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath }/layui/layui.js"></script>
</head>
<body>
	<form class="layui-form">
	  <div class="layui-form-item">
	    <label class="layui-form-label" style="width: 90px;">类型名称</label>
	    <div class="layui-input-block">
	      <input type="text" name="typename" required  lay-verify="required" placeholder="请输入类型名称" autocomplete="off" class="layui-input">
	    </div>
	  </div>
	  <div class="layui-form-item">
	    <label class="layui-form-label" style="width: 90px;">级别</label>
	    <div class="layui-input-block">
	      <select id="typelevel" name="typelevel" lay-filter="typelevel">
	        <option value="0">父级</option>
	        <option value="1">子级</option>
	      </select>
	    </div>
	   </div>
	   
	   <div id="fu" class="layui-form-item" style="display:none">
	    <label class="layui-form-label" style="width: 90px;">父级内容</label>
	    <div class="layui-input-block">
	      <select id="parentid" name="parentid" lay-filter="parentid">
	      
	      </select>
	    </div>
	   </div>
	  
	  <div class="layui-form-item">
	    <div class="layui-input-block">
	      <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
	      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
	    </div>
	  </div>
	</form>
 
<script>
function addParent(){
	
	$.ajax({
		//请求路径
		url:"/rj2002/admin/getParentType",
		//请求方式 get post ...
		type:"post",
		dataType:"json",
		data:{},
		//请求成功后做的事情
		success:function(data){
			data = data.data;
			//添加父级内容
			for(var i=0;i<data.length;i++){
				var option = '<option value="'+data[i].id+'">'+data[i].typename+'</option>';
				$("#parentid").append(option);
			}
			form.render($('#parentid'));
		},
		//请求失败后做的事情
		error:function(XMLHttpRequest,textStatus,errorThrown){
			console.log(XMLHttpRequest)
		}
	});
}
function addType(datas){
	$.ajax({
		//请求路径
		url:"/rj2002/admin/addGoodType",
		//请求方式 get post ...
		type:"post",
		dataType:"json",
		data:datas.field,
		//请求成功后做的事情
		success:function(data){
			layer.alert(data.data,{icon: 1},function(index){ window.location.reload(); });
		},
		//请求失败后做的事情
		error:function(XMLHttpRequest,textStatus,errorThrown){
			console.log(XMLHttpRequest)
		}
	});
}
$(function(e){
	addParent();
})
var layer,form;
layui.use('form', function(){
	form = layui.form;
	layer = layui.layer;
    //提交
	form.on('submit(formDemo)', function(datas){
		addType(datas);
	});
	form.on('select(typelevel)', function(datas){
		if(datas.value == 1){
			//当前为子级元素，需要添加父级内容
			$("#fu").css("display","block")
		}else{
			$("#fu").css("display","none")
		}
		console.log(datas)
	});
	form.on('select(parentid)', function(datas){
		console.log(datas)
	});
});
</script>
</body>
</html>